package org.bitbrushers.jobextractor;

import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.bitbrushers.jobextractor.apinfo.JobIndexPage;
import org.bitbrushers.jobextractor.apinfo.JobOfferPage;
import org.bitbrushers.jobextractor.apinfo.JobPageLink;
import org.bitbrushers.jobextractor.net.ContentHandlerFactoryImpl;
import org.bitbrushers.jobextractor.output.ExcelWriter;
import org.bitbrushers.jobextractor.to.JobOffer;


public class Main {
	
	public static void extractJobsToCSV(String urlStr, String filename, int days) {
		List<JobOffer> jobList = new ArrayList<JobOffer>();
		System.out.println("MESSAGE: Looking for new job offers.");
		JobIndexPage page = new JobIndexPage(urlStr);
		ExcelWriter writer = new ExcelWriter(filename);
		// first get the link list
		List<JobPageLink> linkList = page.extractJobPageLinks();
		DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yy");

		Date today = new Date();
		Date linkDate = null;
		
		int jobPageCount = 0;
		int newJobCount = 0;
		int repeatedJobCount = 0;
		
		// second check if link in linkList is new
		// if not new, process job page and add jobs to jobList
		for (JobPageLink link : linkList) {
			try {
				linkDate = dateFormatter.parse(link.getDate());
			} catch (java.text.ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			long dateDiff = (today.getTime() - linkDate.getTime())/(1000*60*60*24);

			if (dateDiff < 0) {
				continue;
			}
			else if (days > 0 && dateDiff > days) {
				continue;
			}

			JobOfferPage jobOfferPage = new JobOfferPage(link);
			List<JobOffer> newJobList = jobOfferPage.extractJobOffers();
			jobList.addAll(newJobList);
			System.out.println("MESSAGE: Extracted " + newJobList.size()
					+ " jobs from \"" + link.getText() + "\"");
			jobPageCount++;
		}
		
		// third write jobs in jobList to file
		for (JobOffer jobOffer : jobList) {
			if (writer.writeJobOffer(jobOffer)) {
				newJobCount++;
			}
			else {
				repeatedJobCount++;
			}
		}
		writer.close();
		
		if (jobPageCount == 0) {
			System.out.println("MESSAGE: No new jobs available.");
		}
		else {
			System.out.println("MESSAGE: Total " + newJobCount + " new jobs from "
					+ jobPageCount + " page(s).");
			System.out.println("MESSAGE: Repeated findings: " + repeatedJobCount + " job offer(s) not saved.");
			System.out.println("MESSAGE: Wrote \"" + filename + "\".");
			System.out.println("File contents: (c)Apinfo (http://www.apinfo.com)");
		}
	}
	
	
	/**
	 * java Main -index <index_url> -days <number_of_days>
	 * java Main -help
	 * 
	 * 
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String url = null;
		int nDays = 0;
		Options options = new Options();
		Option help = new Option("help", "Print this message.");
		Option index = new Option("index", true, "The page with links to jobs page.");
		Option days = new Option("days", true, "The number of days to retrieve.");

		index.setArgName("indexURL");
		
		index.setArgName("numberOfDays");
		options.addOption(help);
		options.addOption(index);
		options.addOption(days);
		
		CommandLineParser cmdLineParser = new  GnuParser();
        CommandLine cmdLine = null;

        System.err.println("VERSION: 0.4.0");

	    try {
	        // parse the command line arguments
	    	cmdLine = cmdLineParser.parse(options, args, true);
	    }
	    catch( ParseException exp ) {
	        // oops, something went wrong
	        System.err.println( "WARNING: " + exp.getMessage());
	        System.exit(1);
	    }
		
	    if (cmdLine.hasOption("help") || cmdLine.getArgList().size() != 0) {
        	HelpFormatter helpFormatter = new HelpFormatter();
        	helpFormatter.printHelp("java -jar BBJobExtractor.jar [options]", options);
        	System.exit(0);
        }

        if (cmdLine.hasOption("index")) {
        	url = cmdLine.getOptionValue("index");
        }
        else {
			System.out.println("WARNING: No URL provided. Using default.");
			url = "http://www.apinfo3.com/pp10l.htm";
        }

        if (cmdLine.hasOption("days")) {
        	try {
        		nDays = Integer.parseInt(cmdLine.getOptionValue("days"));
        	}
        	catch (NumberFormatException e) {
        		System.out.println("WARNING: Invalid number.");
        		System.exit(1);
        	}
        }
        else {
			System.out.println("WARNING: No number of days provided. Retrieving all that are new.");
			nDays = 0;
        }

		URLConnection.setContentHandlerFactory(new ContentHandlerFactoryImpl());
		Main.extractJobsToCSV(url, "apinfo_jobs.xls", nDays);
		System.out.println("MESSAGE: Doubts, bug reporting, other projects"
				+ " or even donation, send an email");
		System.out.println("MESSAGE: to bitbrushers@gmail.com");

	}
}
